package com.cskaoyan.service;

import com.cskaoyan.bean.bo.GoodsCategoryBO;
import com.cskaoyan.bean.bo.GoodsCommentListBo;
import com.cskaoyan.bean.bo.GoodsListBo;
import com.cskaoyan.bean.bo.GoodsUpdateBo;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.bean.vo.wx.FloorGoodsVo;
import com.cskaoyan.converter.GoodsConverter;
import com.cskaoyan.dao.*;
import com.cskaoyan.utils.StringUtils;
import com.github.pagehelper.PageHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.System;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 杨丰韶
 * @class GoodsServiceImpl
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    private GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    private GoodsProductMapper goodsProductMapper;
    @Autowired
    private OrderGoodsMapper orderGoodsMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    IssueMapper issueMapper;
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    /**
     * 查询商品列表
     *
     * @param goodsParams 查询参数封装类
     * @return GoodsListVo
     */
    @Override
    public GoodsListVo queryGoodsList(GoodsListBo goodsParams) {
        PageHelper.startPage(goodsParams.getPage(), goodsParams.getLimit());

        GoodsExample goodsExample = new GoodsExample();

        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        //按商品序列号查
        if (!StringUtils.isEmpty(goodsParams.getGoodsSn())) {
            criteria.andGoodsSnEqualTo(goodsParams.getGoodsSn());
        }
        //按照商品名模糊查
        if (!StringUtils.isEmpty(goodsParams.getName())) {
            criteria.andNameLike("%" + goodsParams.getName() + "%");
        }
        //只查未删除的
        criteria.andDeletedEqualTo(false);
        //按照add_Time字段降序
        goodsExample.setOrderByClause(goodsParams.getSort() + " " + goodsParams.getOrder()+", update_time desc");
        //查询结果
        List<Goods> goodsList = goodsMapper.selectByExampleWithBLOBs(goodsExample);

        long count = goodsMapper.countByExample(goodsExample);

        return new GoodsListVo(count, goodsList);
    }

    /**
     * 查询商品种类和品牌信息
     *
     * @return
     */
    @Override
    public GoodsCatAndBrandVo queryGoodsCatAndBrand() {
        //查询品牌信息
        List<GoodsCatAndBrandListChildrenVo> brandInfos = brandMapper.selectBrandIdAndName();
        //查询一级商品种类信息
        List<GoodsCatAndBrandListVo> categoryInfos = categoryMapper.selectCategoryParentIdAndName();
        for (GoodsCatAndBrandListVo categoryInfo : categoryInfos) {
            //查询二级商品分类信息
            Integer parentId = categoryInfo.getValue();
            List<GoodsCatAndBrandListChildrenVo> categoryChildrenIdAndName = categoryMapper.selectCategoryChildrenIdAndName(parentId);
            categoryInfo.setChildren(categoryChildrenIdAndName);
        }

        return new GoodsCatAndBrandVo(categoryInfos, brandInfos);
    }

    /**
     * 根据商品id获得商品详情
     *
     * @param id
     * @return
     */
    @Override
    public GoodsDetailVo queryGoodsDetail(Integer id) {


        //查询并封装goods
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        Goods good = goodsMapper.selectByPrimaryKey(id);
        goodsDetailVo.setGoods(good);


        //查询并封装categoryIds
        Category category = categoryMapper.selectCategoryById(good.getCategoryId());
        Integer[] integers = {category.getPid(), good.getCategoryId()};
        goodsDetailVo.setCategoryIds(integers);

        //查询并封装attributes
        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria attributeCriteria = goodsAttributeExample.createCriteria();
        attributeCriteria.andGoodsIdEqualTo(id)
                .andDeletedEqualTo(false);
        List<GoodsAttribute> attributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);
        goodsDetailVo.setAttributes(attributes);

        //查询并封装specifications
        GoodsSpecificationExample specificationExample = new GoodsSpecificationExample();
        GoodsSpecificationExample.Criteria specificationCriteria = specificationExample.createCriteria();
        specificationCriteria.andDeletedEqualTo(false)
                .andGoodsIdEqualTo(id);
        List<GoodsSpecification> specifications = goodsSpecificationMapper.selectByExample(specificationExample);
        goodsDetailVo.setSpecifications(specifications);

        //查询并封装products
        GoodsProductExample productExample = new GoodsProductExample();
        GoodsProductExample.Criteria productCriteria = productExample.createCriteria();
        productCriteria.andGoodsIdEqualTo(id)
                .andDeletedEqualTo(false);
        List<GoodsProduct> products = goodsProductMapper.selectByExample(productExample);
        goodsDetailVo.setProducts(products);
        return goodsDetailVo;

    }

    /**
     * 更新商品信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer changeGood(GoodsUpdateBo goods) {
        //更新goods表信息
        Goods goodsVo = goods.getGoods();
        GoodsExample goodsExampleTarget = new GoodsExample();
        GoodsExample.Criteria goodsExampleTargetCriteria = goodsExampleTarget.createCriteria();
        goodsExampleTargetCriteria.andDeletedEqualTo(false)
                .andIdEqualTo(goodsVo.getId());
        List<Goods> exitedGoodsList = goodsMapper.selectByExample(goodsExampleTarget);
        Goods targetGoods = exitedGoodsList.get(0);
        if (!goodsVo.getGoodsSn().equals(targetGoods.getGoodsSn())) {
            return 500;
        }

        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodsCriteria = goodsExample.createCriteria();
        goodsCriteria.andIdEqualTo(goodsVo.getId());
        goodsMapper.updateByExampleSelective(goodsVo, goodsExample);

        //更新specification表
        List<GoodsSpecification> specifications = goods.getSpecifications();
        if (specifications.size() == 0) {//至少要添加一个规格
            return 501;
        }
        goodsSpecificationMapper.deleteAllByGoodsId(goodsVo.getId());
        for (GoodsSpecification specification : specifications) {

            specification.setGoodsId(goodsVo.getId());
            specification.setId(null);
            specification.setAddTime(new Date(System.currentTimeMillis()));
            specification.setUpdateTime(new Date(System.currentTimeMillis()));
            goodsSpecificationMapper.insertSelective(specification);
        }


        //更新products表
        goodsProductMapper.deleteAllByGoodsId(goodsVo.getId());
        List<GoodsProduct> products = goods.getProducts();
        for (GoodsProduct product : products) {
            product.setId(null);
            Date date = new Date(System.currentTimeMillis());
            product.setAddTime(date);
            product.setUpdateTime(date);
            product.setGoodsId(goodsVo.getId());
            goodsProductMapper.insertSelective(product);
        }

        //更新attribute表
        List<GoodsAttribute> attributes = goods.getAttributes();
        goodsAttributeMapper.deletedAllByGoodsId(goodsVo.getId());
        for (GoodsAttribute attribute : attributes) {

            attribute.setGoodsId(goodsVo.getId());
            attribute.setId(null);
            Date date = new Date(System.currentTimeMillis());
            attribute.setAddTime(date);
            attribute.setUpdateTime(date);
            goodsAttributeMapper.insertSelective(attribute);
        }
        return 200;

    }

    @Override
    public List<OrderGoods> queryOrdersByGoodsId(int goodsId) {
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        if (goodsId != 0) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        return orderGoodsMapper.selectByExample(orderGoodsExample);
    }

    /**
     * 新增商品信息
     *
     * @param goodsUpdate
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addGood(GoodsUpdateBo goodsUpdate) {

        Goods goods = goodsUpdate.getGoods();

        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodsCriteria = goodsExample.createCriteria();
        goodsCriteria.andGoodsSnEqualTo(goods.getGoodsSn());
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
        if (goodsList != null && goodsList.size() > 0) {//商品编号已经存在
            return 500;
        }

        if (goods.getCategoryId() == null) {//商品分类信息必须要选
            return 501;
        }
        List<GoodsSpecification> specifications = goodsUpdate.getSpecifications();
        if (specifications.size() == 0) {//要求至少选择一个规格
            return 502;
        }

        //新增goods
        goods.setAddTime(new Date(System.currentTimeMillis()));
        goods.setUpdateTime(new Date(System.currentTimeMillis()));
        goodsMapper.insertSelective(goods);

        //新增specification表
        for (GoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            specification.setId(null);
            Date date = new Date(System.currentTimeMillis());
            specification.setAddTime(date);
            specification.setUpdateTime(date);
            goodsSpecificationMapper.insertSelective(specification);
        }

        //
        List<GoodsProduct> products = goodsUpdate.getProducts();
        for (GoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            product.setId(null);
            Date date = new Date(System.currentTimeMillis());
            product.setAddTime(date);
            product.setUpdateTime(date);
            goodsProductMapper.insertSelective(product);
        }

        //
        List<GoodsAttribute> attributes = goodsUpdate.getAttributes();
        for (GoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            Date date = new Date(System.currentTimeMillis());
            attribute.setAddTime(date);
            attribute.setUpdateTime(date);
            attribute.setId(null);
            goodsAttributeMapper.insertSelective(attribute);
        }

        return 200;
    }

    /**
     * 根据商品id逻辑删除该商品以及与之相关的信息
     *
     * @param goodsId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer removeGoodsDetail(Integer goodsId) {

        Integer affectedRows = goodsMapper.deleteById(goodsId);

        goodsSpecificationMapper.deleteAllByGoodsId(goodsId);

        goodsProductMapper.deleteAllByGoodsId(goodsId);

        goodsAttributeMapper.deletedAllByGoodsId(goodsId);

        if (affectedRows == 1) {
            return 200;
        }
        return 404;
    }


    @Override
    public List<Goods> queryNewGoodsList() {
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andIsNewEqualTo(true);
        criteria.andDeletedEqualTo(false);
        return goodsMapper.selectByExample(goodsExample);
    }

    @Override
    public List<Goods> queryHotGoodsList() {
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andIsHotEqualTo(true);
        criteria.andDeletedEqualTo(false);
        return goodsMapper.selectByExample(goodsExample);
    }

    @Override
    public List<FloorGoodsVo> queryFloorGoodsList(int num) {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria1 = categoryExample.createCriteria();
        criteria1.andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        ArrayList<FloorGoodsVo> floorGoodsVoList = new ArrayList<>();
        // 分类栏目商品显示数量 cskaoyan_mall_wx_catlog_goods
        for (Category category : categories) {
            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria criteria = goodsExample.createCriteria();
            criteria.andCategoryIdEqualTo(category.getId());
            criteria.andDeletedEqualTo(false);
            List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);

            FloorGoodsVo floorGoodsVo = new FloorGoodsVo();
            floorGoodsVo.setName(category.getName());
            floorGoodsVo.setGoodsList(goodsList);
            floorGoodsVo.setId(category.getId());
            floorGoodsVoList.add(floorGoodsVo);
        }
        // 取0 到 num - 1的元素
        if (num != 0 && num < floorGoodsVoList.size()) {
            return floorGoodsVoList.subList(0, num);
        }
        return floorGoodsVoList;
    }

    @Override
    public List<Brand> queryGoodsBrand() {
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        return brandMapper.selectByExample(brandExample);
    }

    @Override
    public List<Category> queryGoodsCategory() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        return categoryMapper.selectByExample(categoryExample);
    }

    @Override
    public List<OrderGoods> queryOrderGoodsByOrderId(int orderId) {
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        criteria.andDeletedEqualTo(false);
        return orderGoodsMapper.selectByExample(orderGoodsExample);
    }

    @Override
    public Order queryOrderByOrderId(int orderId) {
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIdEqualTo(orderId);
        List<Order> orders = orderMapper.selectByExample(orderExample);
        if (orders.size() == 0) {
            return null;
        }
        return orders.get(0);
    }

    @Override
    public GoodsCommentListVo queryGoodsCommentList(GoodsCommentListBo listBo) {
        PageHelper.startPage(listBo.getPage(), listBo.getLimit());


        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria commentExampleCriteria = commentExample.createCriteria();
        if (!StringUtils.isEmpty(listBo.getUserId())) {
            commentExampleCriteria.andUserIdEqualTo(Integer.valueOf(listBo.getUserId()));
        }
        if (!StringUtils.isEmpty(listBo.getValueId())) {
            commentExampleCriteria.andValueIdEqualTo(Integer.valueOf(listBo.getValueId()));
        }
        commentExampleCriteria.andDeletedEqualTo(false);
        commentExampleCriteria.andTypeEqualTo((byte) 0);
        commentExample.setOrderByClause(listBo.getSort() + " " + listBo.getOrder());

        List<Comment> comments = commentMapper.selectByExample(commentExample);

        long count = commentMapper.countByExample(commentExample);

        return new GoodsCommentListVo(count, comments);
    }

    @Override
    public Integer removeGoodsComment(Integer id) {
        Integer affectedRows = commentMapper.deleteLogicalByCommentId(id);
        if (affectedRows == 1) {
            return 200;
        }
        return 500;
    }

    /**
     * @author thz
     * @title queryGoodsCount
     * @description: 查询商品总数量
     * @param: []
     * @return: com.cskaoyan.bean.vo.GoodsCountVO
     */
    @Override
    public GoodsCountVO queryGoodsCount() {
        GoodsCountVO goodsCountVO = new GoodsCountVO();
        GoodsExample goodsExample = new GoodsExample();
        long count = goodsMapper.countByExample(goodsExample);
        goodsCountVO.setGoodsCount(count);
        return goodsCountVO;
    }

    /**
     * @author thz
     * @title queryWxGoodsCategory
     * @description: 商品类目
     * @param: [id]
     * @return: com.cskaoyan.bean.vo.GoodsCategoryVO
     */
    @Override
    public GoodsCategoryVO queryWxGoodsCategory(Integer id) {
        GoodsCategoryVO goodsCategoryVO = new GoodsCategoryVO();
        CategoryExample brotherCategoryExample = new CategoryExample();

        Category currentCategory = categoryMapper.selectByPrimaryKey(id);
        goodsCategoryVO.setCurrentCategory(currentCategory);

        Category parentCategory;
        if (currentCategory.getPid() != 0) {
            parentCategory = categoryMapper.selectByPrimaryKey(currentCategory.getPid());
        } else {
            parentCategory = currentCategory;
        }
        goodsCategoryVO.setParentCategory(parentCategory);

        brotherCategoryExample.createCriteria().andDeletedEqualTo(false).andPidEqualTo(parentCategory.getId());
        List<Category> brotherCategories = categoryMapper.selectByExample(brotherCategoryExample);
        goodsCategoryVO.setBrotherCategory(brotherCategories);

        return goodsCategoryVO;

    }

    /**
     * @author thz lds
     * @title queryWxGoodsList
     * @description: 商品列表
     * @param: [goodsCategoryBO]
     * @return: com.cskaoyan.bean.vo.WxGoodsListVO
     */
    @Override
    public WxGoodsListVO queryWxGoodsList(GoodsCategoryBO goodsCategoryBO, User user) {
        PageHelper.startPage(goodsCategoryBO.getPage(), goodsCategoryBO.getSize());
        WxGoodsListVO wxGoodsListVO = new WxGoodsListVO();
        if (goodsCategoryBO.getKeyword()!=null||goodsCategoryBO.getSort()!=null||goodsCategoryBO.getOrder()!=null){
            addSearchHistory(goodsCategoryBO, user);
            WxGoodsListVO wxGoodsListVO1 = queryWxGoodsListInOrder(goodsCategoryBO, user, wxGoodsListVO);
            return  wxGoodsListVO1;
        }



        Category category = categoryMapper.selectByPrimaryKey(goodsCategoryBO.getCategoryId());

        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false)
         .andPidEqualTo(goodsCategoryBO.getCategoryId());

        List<Category> categories = categoryMapper.selectByExample(categoryExample);

        wxGoodsListVO.setFilterCategoryList(categories);

        long count = 0;
        List<Goods> goods = new ArrayList<>();
        if (category.getPid() != 0) {
            GoodsExample goodsExample = new GoodsExample();
            goodsExample.createCriteria().andDeletedEqualTo(false)
                    .andCategoryIdEqualTo(goodsCategoryBO.getCategoryId());
            goods = goodsMapper.selectByExample(goodsExample);
            count = goodsMapper.countByExample(goodsExample);
        } else {
            for (Category categoryBean : categories) {
                GoodsExample goodsExample = new GoodsExample();
                goodsExample.createCriteria().andDeletedEqualTo(false).andCategoryIdEqualTo(categoryBean.getId());
                List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
                goods.addAll(goodsList);
                count += goodsMapper.countByExample(goodsExample);
            }
        }

        List<WxGoodsListVO.GoodsListBean> goodsListBeans = GoodsConverter.convertToGoodsListBean(goods);

        wxGoodsListVO.setGoodsList(goodsListBeans);
        wxGoodsListVO.setCount(count);

        return wxGoodsListVO;
    }

    private WxGoodsListVO queryWxGoodsListInOrder(GoodsCategoryBO goodsCategoryBO, User user, WxGoodsListVO wxGoodsListVO) {
        //goods的模糊查询
        String keyword = goodsCategoryBO.getKeyword();
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andNameLike("%"+keyword + "%");
        criteria.andDeletedEqualTo(false);
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        List<WxGoodsListVO.GoodsListBean> goodsListBeans = GoodsConverter.convertToGoodsListBean(goods);

        List<Category> categoriesList = new LinkedList<>();
        for (Goods good : goods) {
            CategoryExample categoryExample = new CategoryExample();
            CategoryExample.Criteria criteria1 = categoryExample.createCriteria();
            criteria1.andIdEqualTo(good.getCategoryId());
            List<Category> categories = categoryMapper.selectByExample(categoryExample);
            categoriesList.addAll(categories);
        }


        //根据goods找类目信息

        wxGoodsListVO.setFilterCategoryList(categoriesList);
        wxGoodsListVO.setGoodsList(goodsListBeans);
        wxGoodsListVO.setCount(goods.size());
        return wxGoodsListVO;
    }

    /**
     * @author: lds
     * @title: addSearchHistory
     * @description: addSearchHistory
     * @param: [goodsCategoryBO, user]
     * @return: void
     */
    private void addSearchHistory(GoodsCategoryBO goodsCategoryBO, User user) {

            SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
            SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
            criteria.andUserIdEqualTo(user.getId());
            criteria.andKeywordEqualTo(goodsCategoryBO.getKeyword());
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.setKeyword(goodsCategoryBO.getKeyword());
            searchHistory.setAddTime(null);

            searchHistory.setUpdateTime(new Date(System.currentTimeMillis()));
            searchHistory.setDeleted(false);
            searchHistory.setFrom("wx");
            int i=0;
            try {
                 i = searchHistoryMapper.updateByExampleSelective(searchHistory, searchHistoryExample);
            }catch (Exception e) {
                i=0;
            }

            if (i==0){
                searchHistory.setUserId(user.getId());
                searchHistory.setAddTime(new Date(System.currentTimeMillis()));
                searchHistory.setUpdateTime(null);
                int i1 = searchHistoryMapper.insertSelective(searchHistory);
            }

    }

    /**
     * @author thz
     * @title queryWxGoodsDetail
     * @description: 商品详情
     * @param: [id]
     * @return: com.cskaoyan.bean.vo.WxGoodsDetailVO
     */
    @Override
    public WxGoodsDetailVO queryWxGoodsDetail(Integer id) {
        WxGoodsDetailVO wxGoodsDetailVO = new WxGoodsDetailVO();
        GoodsDetailVo goodsDetailVo = queryGoodsDetail(id);

        //specificationList
        ArrayList<WxGoodsDetailVO.SpecificationListBean> specificationListBeans = new ArrayList<>();
        List<GoodsSpecification> specifications = goodsDetailVo.getSpecifications();

        outer:
        for (GoodsSpecification specification : specifications) {
            ArrayList<GoodsSpecification> goodsSpecifications = new ArrayList<>();
            goodsSpecifications.add(specification);
            String specificationName = specification.getSpecification();
            for (WxGoodsDetailVO.SpecificationListBean specificationListBean : specificationListBeans) {
                if (specificationName.equals(specificationListBean.getName())){
                    specificationListBean.getValueList().add(specification);
                    continue outer;
                }
            }
            specificationListBeans.add(new WxGoodsDetailVO.SpecificationListBean(specificationName,
                    goodsSpecifications));
        }
        wxGoodsDetailVO.setSpecificationList(specificationListBeans);

        //comment
        WxGoodsDetailVO.CommentBean commentBean = new WxGoodsDetailVO.CommentBean();
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andDeletedEqualTo(false).andValueIdEqualTo(id);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        long count = commentMapper.countByExample(commentExample);
        commentBean.setCount(count);
        ArrayList<WxGoodsDetailVO.CommentBean.DataBean> dataBeans = new ArrayList<>();
        for (Comment comment : comments) {
            User user = userMapper.selectByPrimaryKey(comment.getUserId());
            dataBeans.add(new WxGoodsDetailVO.CommentBean.DataBean(comment.getAddTime(), user.getNickname(), comment.getId(), user.getAvatar(),
                    comment.getContent(), comment.getPicUrls()));
        }
        commentBean.setData(dataBeans);
        wxGoodsDetailVO.setComment(commentBean);

        //brand
        Brand brand = brandMapper.selectByPrimaryKey(goodsDetailVo.getGoods().getBrandId());
        wxGoodsDetailVO.setBrand(brand);

        //info
        wxGoodsDetailVO.setInfo(goodsDetailVo.getGoods());

        //productList
        wxGoodsDetailVO.setProductList(goodsDetailVo.getProducts());

        //attribute
        wxGoodsDetailVO.setAttribute(goodsDetailVo.getAttributes());

        //issue
        IssueExample issueExample = new IssueExample();
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        wxGoodsDetailVO.setIssue(issues);

        //groupon
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.createCriteria().andDeletedEqualTo(false).andGoodsIdEqualTo(goodsDetailVo.getGoods().getId());
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        wxGoodsDetailVO.setGroupon(grouponRules);

        //userHasCollect
        CollectExample collectExample = new CollectExample();
        collectExample.createCriteria().andValueIdEqualTo(id);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if (collects.size() == 0) {
            wxGoodsDetailVO.setUserHasCollect(0);
        } else {
            wxGoodsDetailVO.setUserHasCollect(collects.get(0).getType());
        }

        return wxGoodsDetailVO;
    }

    /**
     * @author thz
     * @title queryRelatedGoods
     * @description: 相关商品
     * @param: [id]
     * @return: com.cskaoyan.bean.vo.WxGoodsRelatedVo
     */
    @Override
    public WxGoodsRelatedVo queryRelatedGoods(Integer id) {
        WxGoodsRelatedVo wxGoodsRelatedVo = new WxGoodsRelatedVo();
        ArrayList<WxGoodsRelatedVo.GoodsListBean> goodsListBeans = new ArrayList<>();
        GoodsDetailVo goodsDetailVo = queryGoodsDetail(id);
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andDeletedEqualTo(false).andCategoryIdEqualTo(goodsDetailVo.getGoods().getCategoryId());
        List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        if (goods.size() <= 6) {
            for (Goods good : goods) {
                goodsListBeans.add(new WxGoodsRelatedVo.GoodsListBean(good.getId(), good.getName(), good.getBrief(),
                        good.getPicUrl(), good.getIsNew(), good.getIsHot(), good.getCounterPrice(), good.getRetailPrice()));
            }
        } else {
            for (int i = 0; i < 6; i++) {
                int index = (int) (Math.random() * goods.size());
                Goods good = goods.remove(index);
                goodsListBeans.add(new WxGoodsRelatedVo.GoodsListBean(good.getId(), good.getName(), good.getBrief(),
                        good.getPicUrl(), good.getIsNew(), good.getIsHot(), good.getCounterPrice(), good.getRetailPrice()));
            }
        }
        wxGoodsRelatedVo.setGoodsList(goodsListBeans);
        return wxGoodsRelatedVo;
    }

    @Override
    public GoodsProduct queryGoodsProductById(int productId) {
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        goodsProductExample.createCriteria().andDeletedEqualTo(false).andIdEqualTo(productId);

        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);
        if (goodsProducts.size() != 0) {
            return goodsProducts.get(0);
        }
        return null;
    }

    @Override
    public int changeGoodsProductById(GoodsProduct goodsProduct, int id) {
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        goodsProductExample.createCriteria().andIdEqualTo(id);
        return goodsProductMapper.updateByExampleSelective(goodsProduct, goodsProductExample);
    }

}
